<script setup lang="ts">
import cpNavBar from "@/components/cpNavBar.vue";
import { Patient } from "@/types/user";
import { ref } from "vue";
import { nameRules, idCardRules } from "@/utils/rules";
import { addPatientAPI, editPatientAPI } from "@/api/user";
import { useRouter } from "vue-router";
import { useRoute } from "vue-router";
const patient = ref({
  name: "",
  idCard: "110101198307212600",
  gender: 1,
  defaultFlag: 0,
});
const router = useRouter();
const route = useRoute();
console.log(route.query);
const { id, name, idCard, gender, defaultFlag } = route.query;
patient.value = { id, name, idCard, gender, defaultFlag };
const add = async () => {
  patient.value.defaultFlag = Number(patient.value.defaultFlag);
  // console.log(patient.value);
  if (patient.value?.id) {
    // console.log(patient.value);
    const res = await editPatientAPI(patient.value);
    console.log(res);
  } else {
    const res = await addPatientAPI(patient.value);
    console.log(res);
  }
  router.push("/user/patient");
};
</script>
<template>
  <cpNavBar :title="patient.id ? '编辑患者' : '添加患者'" rightText="保存" @child-event="add"></cpNavBar>
  <div class="add-form">
    <van-form>
      <van-field v-model="patient.name" :rules="nameRules" label="真实姓名" placeholder="请输入真实姓名" type="tel" />
      <van-field v-model="patient.idCard" :rules="idCardRules" label="身份证号" placeholder="请输入身份证号" type="tel" />

      <van-field name="radio" label="性别">
        <template #input>
          <van-radio-group v-model="patient.gender">
            <van-radio name="1" checked-color="#07c160">男</van-radio>
            <van-radio name="0" checked-color="#07c160">女</van-radio>
          </van-radio-group>
        </template>
      </van-field>

      <van-field name="checkbox" label="默认就诊人">
        <template #input>
          <van-checkbox v-model="patient.defaultFlag" shape="square"></van-checkbox>
        </template>
      </van-field>
    </van-form>
  </div>
</template>
<style scoped>
.add-form {
  margin-top: 30px;
}
</style>